package com.integrate.rabbitmq.controller;

import com.integrate.rabbitmq.porducer.AckSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 刘志强
 * @date 2020/11/13 14:05
 */
@RestController
@Slf4j
@CrossOrigin
public class RabbitController {

    @Autowired
    private AckSender ackSender;

    /**
     * 消息放入队列
     * @param msg
     * @return
     */
    @GetMapping("sendMsg")
    public String sendMsg(String msg) {
        ackSender.convertAndSend("queue", msg);
        return "已发送";
    }

    /**
     * 消息放入交换机
     * @param msg
     * @return
     */
    @GetMapping("sendExchange")
    public String sendExchange(String msg) {
        ackSender.convertAndSend("exchange", "exchange.queue", msg);
        return "已发送";
    }

    /**
     * 消息放入交换机
     * @param msg
     * @return
     */
    @GetMapping("sendExchangeRoute")
    public String sendExchangeRoute(String msg) {
        ackSender.convertAndSend("exchange", "exchange.xxx", msg);
        return "已发送";
    }

    /**
     * 消息放入广播交换机
     * @param msg
     * @return
     */
    @GetMapping("sendFanoutMsg")
    public String sendFanoutMsg(String msg) {
        ackSender.convertAndSend("fanoutExchange", null,msg);
        return "已发送";
    }

    /**
     * 像队列发送消息并设置 TTL(过期事件)
     * @param msg
     * @return
     */
    @GetMapping("convertAndSendDelay")
    public String convertAndSendDelay(String msg,Long ttl) {
        ackSender.convertAndSendDelay("delayQueue",msg,ttl);
        return "已发送";
    }
}
